package mqtt;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MqCLient {
    public static void main(String[] args) {


        String broker = "tcp://broker.emqx.io:1883";
        String topic = "asdfsdfa";
        String clientid = "publish_clien";

        String content = "各位好！";
        int qos = 0;

        MqttClient client;


            try {
                client = new MqttClient(broker, clientid, new MemoryPersistence());
                MqttConnectOptions options = new MqttConnectOptions();
                options.setConnectionTimeout(60);
                options.setKeepAliveInterval(60);
                //连接
                client.connect(options);
                MqttMessage message = new MqttMessage(content.getBytes());
                message.setQos(qos);
                // 发布消息
                client.publish(topic, message);

                // 关闭连接
                client.disconnect();
                client.close();
            } catch (MqttException e) {

                throw new RuntimeException(e);
            }


        //连接参数
    }


}
